<?php
// 应用公共文件
use app\model\SidebarNavClass as NavClassModel;
use app\model\SidebarNavDetail as NavDetailModel;
use think\facade\Session;

//获取导航菜单信息
function GetNav()
{

    //如果没有Session
    if (Session::has('nav_class') == "") {
        Session::flash('nav_class','1,1');
    }
    //输出大类导航数组
    $nav_class_id = explode(",",Session::get('nav_class'));
    $navMenu = NavClassModel::order('order')->select($nav_class_id);


    //如果没有Session
    if (Session::has('nav_detail') == "") {
        Session::flash('nav_detail','1,1');
    }
    //输出细类导航数组
    $nav_detail_id = explode(",",Session::get('nav_detail'));
    
	for ($x=0; $x<count($navMenu); $x++) {
        //取出大类导航的id
	    $class_id = $navMenu[$x]['id'];

        //查询有权限的属于这个topid的子菜单
        $nav = NavDetailModel::where('topID',$class_id)->order('order')->select($nav_detail_id);
        
        //循环输出子菜单组装url
        for ($i=0; $i < count($nav); $i++) { 
            $url = $nav[$i]['controller']."/".$nav[$i]['action'];
            $nav[$i]['link'] = url($url);
        }

        //将子菜单组装进大菜单
	    $navMenu[$x]['nav'] = $nav;
	}
	return $navMenu;
}

//获取周期首尾日时间戳,传入值为文本格式的数组
function DateInterval($daterange)
{

        //设置默认时间
        if(empty($daterange)){
            $now = strtotime(date("Y-m-d"));
            $daterange[0] = strtotime("-1 week +1 day",$now);
            $daterange[1] = strtotime(date("Y-m-d 23:59:59"));//本周期结束时间
        }else{
            $daterange = explode(' - ', $daterange);
            $daterange[0] = strtotime(date("Y/m/d 00:00:00",strtotime($daterange[0])));//本周期开始时间
            $daterange[1] = strtotime(date("Y/m/d 23:59:59",strtotime($daterange[1])));//上周期结束时间
        }

        //本周期开始时间
        $returnDate['thisStart'] = $daterange[0];
        //本周期结束时间
        $returnDate['thisEnd'] = $daterange[1];

        //判断日期长度
        $datelength = $daterange[1] - $daterange[0];

        //上周期结束时间
        $returnDate['lastEnd'] = $daterange[0] - 1;
        //上周期开始时间
        $returnDate['lastStart'] = $returnDate['lastEnd'] - $datelength;

        //本周期相差的天数
        $thisStartObject = new DateTime(date('Y-m-d',$returnDate['thisStart']));
        $thisEndObject = new DateTime(date('Y-m-d',$returnDate['thisEnd']));
        $returnDate['maxDays'] = $thisStartObject->diff($thisEndObject)->days + 1;

        return $returnDate;
}

// 用于手机端登录模块跳转
function isMobile($request) 
{

    $server = $request->server();

    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($server['HTTP_X_WAP_PROFILE'])){
        return true;
    }
    //此条摘自TPM智能切换模板引擎，适合TPM开发
    if(isset ($server['HTTP_CLIENT']) &&'PhoneClient'==$server['HTTP_CLIENT']){
        return true;
    }
    //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($server['HTTP_VIA'])){
        //找不到为flase,否则为true
        return stristr($server['HTTP_VIA'], 'wap') ? true : false;
    //判断手机发送的客户端标志,兼容性有待提高
    }
    if (isset ($server['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
        'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
        //从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($server['HTTP_USER_AGENT']))){
            return true;
        }
    }
    //协议法，因为有可能不准确，放到最后判断
    if (isset ($server['HTTP_ACCEPT'])) {
    // 如果只支持wml并且不支持html那一定是移动设备
    // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($server['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($server['HTTP_ACCEPT'], 'text/html') === false || (strpos($server['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($server['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}

//高德地图地理位置转换成编码
function geoAddressCode($address,$city)
{   
    //设置高德appkey
    $key = 'eaf39662425acbd507e7675e1f5cbbda';
    // *
    // * url:https://restapi.amap.com/v3/geocode/geo?address=北京市朝阳区阜通东大街6号&output=XML&key=<用户的key>
    // * output（XML/JSON）用于指定返回数据的格式
     
    $url = "https://restapi.amap.com/v3/geocode/geo?output=JSON&key=".$key."&address=".$address."&city=".$city;

    //返回数据"status":"是否成功","info":"失败信息", "count":"返回计数", "geocodes":数组['location':坐标点]
    //返回数据

    // 执行请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($data, true);
    return $result;
}
   

//----The end----
